﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;

namespace BasicForms
{
    public partial class FrmSupportPrintableComponents : BasicForms.FrmClose
    {
        public FrmSupportPrintableComponents()
        {
            InitializeComponent();
        }

        bool _PrintTenCongTy;
        bool _PrintDiaChiDienThoai;
        string _Title = "";
        string _More1 = "";
        string _More2 = "";
        string _Footer1 = "";
        string _Footer2 = "";

        protected void PrintWithHeader(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai, string title, string more1)
        {
            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = more1;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        protected void PrintWithHeader(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai, string title, string more1, string more2)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = more1;
            _More2 = more2;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);

            //printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea);
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        protected void PrintWithHeaderAndFooter(bool useLandscapeView, IPrintable printableComponent, bool printTenCongTy, bool printDiaChiDienThoai,
            string title, string header1, string header2,
            string footer1, string footer2)
        {
            PageHeaderFooter phf = printableComponentLink.PageHeaderFooter as PageHeaderFooter;
            phf.Footer.Content.Clear();
            phf.Footer.Content.AddRange(new string[] { "", "", "Trang [Page #]" });
            phf.Footer.LineAlignment = BrickAlignment.Center;

            _PrintTenCongTy = printTenCongTy;
            _PrintDiaChiDienThoai = printDiaChiDienThoai;
            _Title = title;
            _More1 = header1;
            _More2 = header2;
            _Footer1 = footer1;
            _Footer2 = footer2;

            printableComponentLink.Component = printableComponent;
            printableComponentLink.PaperKind = System.Drawing.Printing.PaperKind.A4;
            printableComponentLink.Landscape = useLandscapeView;
            printableComponentLink.CreateReportHeaderArea += new CreateAreaEventHandler(printableComponentLink_CreateReportHeaderArea);
            printableComponentLink.CreateReportFooterArea += new CreateAreaEventHandler(printableComponentLink_CreateReportFooterArea);
            printableComponentLink.CreateDocument();
            printableComponentLink.ShowPreview();
        }

        private void printableComponentLink_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
        {
            int currentY = 0;

            if (_PrintTenCongTy)
            {
                string tenCongTy = AccountingLibs.Config.TenDoanhNghiep;
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rectCongTy = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 20);
                currentY += 20;
                e.Graph.DrawString(tenCongTy, Color.Black, rectCongTy, BorderSide.None);
            }
            if (_PrintDiaChiDienThoai)
            {
                string diaChi = string.Format("Địa chỉ: {0}. ĐT: {1}", AccountingLibs.Config.DiaChi, AccountingLibs.Config.DienThoai);
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rectDiaChi = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 30);
                currentY += 30;
                e.Graph.DrawString(diaChi, Color.Black, rectDiaChi, BorderSide.None);
            }
            if (_Title.Length > 0)
            {
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
                e.Graph.Font = new Font("Tahoma", 14, FontStyle.Bold);
                RectangleF rec = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 30);
                currentY += 30;
                e.Graph.DrawString(_Title, Color.Black, rec, BorderSide.None);
            }
            if (_More1.Length > 0)
            {
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rect = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 30);
                currentY += 30;
                e.Graph.DrawString(_More1, Color.Black, rect, BorderSide.None);
            }
            if (_More2.Length > 0)
            {
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rect = new RectangleF(0, currentY, e.Graph.ClientPageSize.Width, 30);
                currentY += 30;
                e.Graph.DrawString(_More2, Color.Black, rect, BorderSide.None);
            }
        }
        private void printableComponentLink_CreateReportFooterArea(object sender, CreateAreaEventArgs e)
        {
            e.Graph.BackColor = Color.White;
            if (_Footer1.Length > 0)
            {
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rect = new RectangleF(0, 30, 410, 90);
                e.Graph.DrawString(_Footer1, Color.Black, rect, BorderSide.None);
            }
            if (_Footer2.Length > 0)
            {
                e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
                e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Regular);
                RectangleF rect = new RectangleF(600, 30, 300, 90);
                e.Graph.DrawString(_Footer2, Color.Black, rect, BorderSide.None);
            }
        }
    }
}
